package top.healthylife.springbootdemo01.controller;/*
 *@Author GaoZeXi
 *@Created time 2020/4/29 9:49
 *@Description:
 * Step by Step  and Stand on,  You Are The Best Investment!!!
 */

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.healthylife.springbootdemo01.constant.MessageConstant;
import top.healthylife.springbootdemo01.domain.Result;
import top.healthylife.springbootdemo01.domain.Student;
import top.healthylife.springbootdemo01.service.StudentService;

import java.util.List;

@RestController
@RequestMapping("/stu")
public class StudentController {
    @Autowired
    private StudentService studentService;

    @RequestMapping("/findAll")
    public Result findAll(){
        try {
            List<Student> studentList = studentService.findAll();
            return new Result(true, MessageConstant.QUERY_STUDENT_SUCCESS,studentList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_STUDENT_FAIL);
        }
    }
    @RequestMapping("/add")
    public Result add(@RequestBody Student student){
        try {
            studentService.add(student);
            return new Result(true, MessageConstant.ADD_STUDENT_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_STUDENT_FAIL);
        }
    }
    @RequestMapping("/update")
    public Result updateById(Integer id ,String field,String value){
        try {
            studentService.update(id,field,value);
            return new Result(true, MessageConstant.UPDATE_STUDENT_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.UPDATE_STUDENT_FAIL);
        }
    }

    @RequestMapping("/delete")
    public Result deleteById(Integer id){
        try {
            studentService.delete(id);
            return new Result(true, MessageConstant.DELETE_STUDENT_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_STUDENT_FAIL);
        }
    }
}
